package com.kavin.checknetwork;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    NetWorkStateReceiver netWorkStateReceiver;
    private Context context;
    private TextView sdk_int;
    private TextView network_23;
    private TextView network_23orNew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        network_23 = findViewById(R.id.network_23);
        sdk_int = findViewById(R.id.sdk_int);
        network_23orNew = findViewById(R.id.network_23orNew);

        sdk_int.setText("Build.VERSION.SDK_INT：" + Build.VERSION.SDK_INT);

//        registerReceiver();
    }


    private void nregisterReceiver() {
        unregisterReceiver(netWorkStateReceiver);
        System.out.println("注销");
    }


    private void registerReceiver() {
        if (netWorkStateReceiver == null) {
            netWorkStateReceiver = new NetWorkStateReceiver();
        }

        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkStateReceiver, filter);
        System.out.println("注册");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nregisterReceiver();
    }


    //检测当前的网络状态

    //API版本23以下时调用此方法进行检测
    //因为API23后getNetworkInfo(int networkType)方法被弃用
    public void checkState_23(View view) {
        //步骤1：通过Context.getSystemService(Context.CONNECTIVITY_SERVICE)获得ConnectivityManager对象
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        //步骤2：获取ConnectivityManager对象对应的NetworkInfo对象
        //NetworkInfo对象包含网络连接的所有信息
        //步骤3：根据需要取出网络连接信息
        //获取WIFI连接的信息
        NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        Boolean isWifiConn = networkInfo.isConnected();

        //获取移动数据连接的信息
        networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        Boolean isMobileConn = networkInfo.isConnected();

        network_23.setText("Wifi是否连接:" + isWifiConn + "\n" + "移动数据是否连接:" + isMobileConn);

        //也可以同时获取，等同什么两个
        //NetworkInfo[] allNetworkInfo = connMgr.getAllNetworkInfo();
    }

    // API 23及以上时调用此方法进行网络的检测，下于23调用抛异常 InvocationTargetException
    // getAllNetworks() 在API 21后开始使用
    //步骤非常类似
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void checkState_23orNew(View view) {
        //获得ConnectivityManager对象
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        //获取所有网络连接的信息
        Network[] networks = connMgr.getAllNetworks();
        //用于存放网络连接信息
        StringBuilder sb = new StringBuilder();
        //通过循环将网络信息逐个取出来
        for (int i = 0; i < networks.length; i++) {
            //获取ConnectivityManager对象对应的NetworkInfo对象
            NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
            sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
        }

        boolean empty = TextUtils.isEmpty(sb.toString());
        network_23orNew.setText(empty ? "网络无连接" : sb.toString());
    }

    public void goNetworkActivity(View view) {
        startActivity(new Intent(this, NetworkActivity.class));
    }
}
